<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>

<html>
	<head>
		<title>设置流程</title>
		<%@ include file="/common/meta.jsp"%>
		<%@ include file="/common/jquery-ui-inc.jsp"%>
        
                
		<script type="text/javascript">
			//提交数据数组
			var submitArray = [];
			var roleDataFlag = false ;
			var actionDataFlag = false ;//2016-02-02
	    	var roleData = null;
	    	var actionData = null;//2016-02-02
	    	var sysEname="${sysEname}";
			//var fileName="${uploadFileName}";
			var deployId="${deployId}";
	    	var deploymentId = "${deploymentId}";
	    	

			
			//-----------------角色Begin---------------
			  function getRoles(xmlRoles) {
			  
			     //已选择的角色
			     var clutemps = null;
				 if("" != xmlRoles && null !=xmlRoles &&  typeof(xmlRoles) != "undefined") {
				    clutemps = xmlRoles;
				 }
			  
		         $.ajax({
		             type: "POST",
		             url: "${ctx}/flow/core/flow-deploy!getRoleListJson.action?dd="+new Date().getTime(),
		             data: "orgId=${orgId}",
		             dataType: "json",
		             error: function (XMLHttpRequest, textStatus, errorThrown) {
		                 alert(textStatus);
		                 $.messager.progress('close');
		             },
		             success: function (data) {
		               $("#role").combogrid('clear');	
			    		roleData = data;
			    		$($('#role').combogrid('grid')).datagrid('loadData',roleData);
			    		roleDataFlag = true;
		                if(clutemps != null && clutemps != "" && typeof(clutemps) != "undefined"){
		                  $("#role").combogrid('setValues',clutemps);	
		                }
		             }
		         });
			}
			
			
			
		//初始化角色Grid
			function initRole(){
				$('#role').combogrid({
						panelWidth:400,
						multiple:true,
						idField:'roleId',
						textField:'roleCname',
						fitColumns:true,
						editable:false,
						singleSelect:true,
						onClickRow:function(rowIndex,rowData){
						    $("#role").combogrid('clear');	
						    $("#role").combogrid('setValue',rowData.roleId);	
						    $("#actionMsgSpan").hide();
				            $("#actionSpan").show();
						    getActions(rowData.roleId);
	 					},
						columns:[[
							{field:'roleEname',title:'角色标识',width:80},
							{field:'roleCname',title:'角色名称',width:80},
							{field:'roleId',title:'角色ID',width:240}
						]]
					});
			}
			//----------------------角色End----------------------
			
				
			//-----------------------资源Begin-----------------------------
			function getActions(roleId,xmlActions) {
				  /*  if(""==$.trim(roleId) || null == roleId ||typeof(roleId)=="undefined"){
				    $.messager.alert('提示','参数roleId为空','warning');
				   } */
			    //已选择的角色
			     var clutemps = null;
				 if("" != xmlActions && null !=xmlActions &&  typeof(xmlActions) != "undefined") {
				    clutemps = xmlActions;
				 }
		         $.ajax({
		             type: "POST",
		             url: "${ctx}/flow/core/flow-deploy!getActionListJson.action?dd="+new Date().getTime(),
		             data: "roleId=" + roleId+"&sysEname="+$("#sysEname").val(),
		             dataType: "json",
		             error: function (XMLHttpRequest, textStatus, errorThrown) {
		                 alert(textStatus);
		                 $.messager.progress('close');
		             },
		             success: function (data) {
		                $("#actionAct").combogrid('clear');	
			    		actionData = data;
					    $($('#actionAct').combogrid('grid')).datagrid('loadData',actionData);
						if(clutemps != null && clutemps != "" && typeof(clutemps) != "undefined"){
		                  $("#actionAct").combogrid('setValues',clutemps);	
		                }
		             }
		         });
			}
			function initActions(){
				$('#actionAct').combogrid({
						panelWidth:400,
						multiple:true,
						singleSelect :true,
						idField:'actId',
						textField:'fullCname',//--
						fitColumns:true,
						editable:false,
						onClickRow:function(rowIndex,rowData){
						  //alert(rowData.actId);
						  $("#actionAct").combogrid('clear');	
						  $("#actionAct").combogrid('setValue',rowData.actId);	
						  $("#action").val(rowData.actUrl);
						  $("#sysEname").val(rowData.sysEname);
	 					},
						columns:[[
							{field:'fullCname',title:'中文全称',width:370}
						]]
					});
			}
			//-----------------------资源End-----------------------------
			
			
			
			$(function(){
				
					$('#nodeListId').datagrid( {
						url : '${ctx}/flow/core/flow-deploy!updateNodes.action?deploymentId=${deploymentId}',
						loadMsg : '数据载入中，请稍后……',
						nowrap : false,
						collapsible : false,
						fitColumns : true,
						singleSelect : true,
						remoteSort : false,
						toolbar:[{
							text:"修改流程",
							iconCls:"icon-save",
							handler:function(){
								var len = $("#nodeListId").datagrid("getRows").length;
								doSubmit(len);
							}
							
						},{
							text : '流程定义更改',
							iconCls : 'icon-add',
							handler : function() {
								deployEdit();
								//reload(); 
							}
						}],
						idField : 'id',
						columns : [ [ {
							field : 'id',
							title : '序号',
							width : 220,
							hidden:true,
							sorter : function(a, b) {
								return (a > b ? 1 : -1);
							}
						}, {
							field : 'name',
							title : '节点名',
							width : 200,
							sorter : function(a, b) {
								return (a > b ? 1 : -1);
							}
						},{
							title : '操作',
							field : 'cz',
							width : 100,
							formatter:function(value,row,index){
								return '<a href="javascript:designNode(\''+row.id+'\',\''+row.name+'\',\''+row.left+'\',\''+row.top+'\',\''+row.width+'\',\''+row.height+'\')" >设置</a>';
							}
						}] ],
						onClickRow:function(rowIndex, rowData){
							setPicPosition(rowData);
						}
					});
				    initRole();
		    		initActions();
			});
			
			function closeBusProcessWin(){
				$('#editWin').window('close');
				window.location.reload();
			}
			
			
			//流程更改
			function deployEdit(){
				$('#editWin').window('open');
				var url = '${ctx}/flow/core/flow-deploy!toEdit.action?deploymentId='+deploymentId+'&deployId='+deployId+"&operType=${operType}";
				$("#businessProFrame").attr("src",url);
			}
			
			//设置覆盖图位置
			function setPicPosition(row){
				var w = {"left":row.left,"top":row.top,"width":row.width,"height":row.height};
				$("#squareId").css(w);
				$("#squareId").show();
			}
			
			//设置节点
			function designNode(id,name,left,top,width,height){
				clearForm();
				$("#id").val(id);
				$("#nodeNameId").text(name);
				$.ajax( {
							type : "post",
							url : "${ctx}/flow/core/flow-deploy!getXmlInfo.action?sysEname="+sysEname+"&deploymentId="+deploymentId+"&nodeName="+ encodeURI(encodeURI(name)),
							beforeSend : function(XMLHttpRequest) {
								//ShowLoading();
							},
							success : function(data, textStatus) {
								var result = jQuery.parseJSON(data);
								if(result.flag=="F"){
								 	$.messager.alert('设置结果',data.msg);
								}else if(result.flag=="T"){
		 							if(result.action!=undefined){
		 								//$("#action").val(result.action+"_"+result.taskType);
		 								$("#action").val(result.action);
		 								$("#sysEname").val(result.sysEname);
									 	$("input[name=taskType][value="+result.taskType+"]").attr("checked",true);
										$("input[name=taskExeType][value="+result.taskExeType+"]").attr("checked",true);
									 	$("#keepDays").val(result.keepDays);
									 	$("input[name=crossDept][value="+result.crossDept+"]").attr("checked",true);
									 	var roleValue=result.roleValue;
									 	var actionActId=result.actionActId;
									 	
										if(roleValue!=""){
											$("input[name=type][value=role]").attr("checked",true);
									 		 $("#actionMsgSpan").hide();
				 							 $("#actionSpan").show();
									 		getRoles(roleValue.split(","));
										    getActions(roleValue,actionActId.split(","));
										}else{
										    $("#actionMsgSpan").hide();
				 							$("#actionSpan").show();
										    getActions(null,actionActId.split(","));
										    $("input[name=type][value=owner]").attr("checked",true);
										    $('#rolespan').hide();
										}
										
										
									 	var taskType=$(":radio[name='taskType']:checked").val();
										if(taskType=="2"){//如果是自处理任务
											$("input[name='type']").attr("disabled","disabled"); 
											$("input[name='taskExeType']").attr("disabled","disabled"); 
											$("#keepDays").attr("disabled","disabled"); 
											$("input[name='crossDept']").attr("disabled",true);
										}else{
										   $("input[name='type']").attr("disabled",false); 
											$("#keepDays").attr("disabled",false); 
											if(roleValue !=""){
				                               $("input[name='crossDept']").attr("disabled",false); 
											   $("input[name='taskExeType']").attr("disabled",false);
											}else{
											   $("input[name='crossDept']").attr("disabled",true); 
				                               $("input[name='taskExeType']").attr("disabled",true); 
											}
										}
		 							
		 							}else{
									  $("input[name=type][value=role]").attr("checked",true);
									  queryRole();
								    }

							}
								
								
							},
							complete : function(XMLHttpRequest, textStatus) {
								//HideLoading();
						},
						error : function() {
							//请求出错处理
						}
						});
				$("#designProcessWin").window("open");
			}
			
			
			
			
			//设置数据
			function doSet(){
				var taskType=$(":radio[name='taskType']:checked").val();
				var flg = checkData(taskType);
				if(flg){
					var id = $("#id").val();
					var nodeNameId = $("#nodeNameId").text();
					//var action = $("#action").val().split("_")[0];
					var action = $("#action").val();
					var actionActId = null;
					var type = $(":radio[name='type']:checked").val();
					var ownerValue = null;
					var roleValue =null;
					var sysEname=$("#sysEname").val();
					var taskType = $(":radio[name='taskType']:checked").val();
					var taskExeType = $(":radio[name='taskExeType']:checked").val();
					var keepDays=$("#keepDays").val();
					var crossDept = $(":radio[name='crossDept']:checked").val();
					
					actionActId = $("#actionAct").combogrid('getValue');
					roleValue = $("#role").combotree('getValues');
					
					var strjson = "[";
						strjson = strjson + "{";
						strjson = strjson + "id:'" + id + "',";
						strjson = strjson + "nodeNameId:'" + nodeNameId + "',";
						strjson = strjson + "action:'" + action + "',";
						strjson = strjson + "actionActId:'" + actionActId + "',";
						strjson = strjson + "type:'" + type + "',";
						if(type == 'role'){
						  strjson = strjson + "ownerValue:'',";
						  strjson = strjson + "roleValue:'" + roleValue+ "',";
						}
						if(type == 'owner'){
						 strjson = strjson + "ownerValue:'owner',";
						 strjson = strjson + "roleValue:'',";
						}
						strjson = strjson + "sysEname:'" +sysEname + "',";
						strjson = strjson + "taskType:'" +taskType + "',";
						strjson = strjson + "taskExeType:'" +taskExeType + "',";
						strjson = strjson + "keepDays:'" +keepDays + "',";
						strjson = strjson + "crossDept:'" +crossDept;
						strjson = strjson + "'},";
					strjson = strjson + "]";
			
					$.ajax( {
							type : "post",
							url : "${ctx}/flow/core/flow-deploy!updateXml.action?sysEname="+sysEname+"&deploymentId="+deploymentId+"&nodeName="+encodeURI(encodeURI(nodeNameId))+"&jsonnodes="+ strjson,
							beforeSend : function(XMLHttpRequest) {
								//ShowLoading();
							},
							success : function(data, textStatus) {
								var result = jQuery.parseJSON(data);
								if(result.flag=="F"){
								 	$.messager.alert('设置结果',data.msg);
								}else if(result.flag=="T"){
								 	$.messager.alert('设置结果',"设置信息修改成功");
								}
								
								
							},
							complete : function(XMLHttpRequest, textStatus) {
								//HideLoading();
						},
						error : function() {
							//请求出错处理
						}
						});
					
					clearForm();
					closeWin();
				}
			}
			
			
			function doSubmit(size){
				var params="deploymentId="+deploymentId+"&deployId="+deployId+"&operType=${operType}";
				var len = submitArray.length;
				var url="${ctx}/flow/core/flow-deploy!update.action?"+ params;
				this.location=url;
			}
			
			//清空form
			function clearForm(){
					initActions();
			        $("#actionMsgSpan").show();
				 	$("#actionSpan").hide();
					$("#role").combogrid('clear');
					$("#keepDays").val("");
					$("input[name=type][value=role]").attr("checked",true);
					$("input[name=taskType][value=1]").attr("checked",true);
					$("input[name=taskExeType][value=1]").attr("checked",true);
					$("input[name=crossDept][value=1]").attr("checked",true);
					setTaskType();
					
			}
					// 验证数据
			function checkData(taskType) {
			    var id = document.getElementById("id");
			    if ( id.value == "" ) {
			         $.messager.alert('提交结果',"节点ID不能为空")
			        id.focus();
			        return false;
			    };
			     if("1"==taskType){//只有待办任务才选择权限类型
			    	//var user = $("#user").combogrid('getValues');
			    	var role = $("#role").combogrid('getValues');
			    	var type = $(":radio[name='type']:checked").val();
			    	if(type == "role" && role==""){
			    		$.messager.alert('提交结果',"请选择角色");
						return false;
			    	}
			    }
			     var actionActId = $("#actionAct").combogrid("getValue");
			     
			    if (actionActId == "" ||actionActId==undefined || actionActId ==null) {
			        $.messager.alert('提交结果失败',"请选择form表单");
			        action.focus();
			        return false;
			    };
			    var action = document.getElementById("action");
			    if ( action.value == "" || action==undefined || action ==null) {
			        $.messager.alert('提交结果',"form表单资源地址为空");
			        action.focus();
			        return false;
			    };
			    
			    var sysEname = $("#sysEname").val();
			    if (sysEname == ""  || sysEname==undefined  || sysEname ==null ) {
			        $.messager.alert('提交结果失败',"系统名sysEname为空");
			        action.focus();
			        return false;
			    };
			    
			    return true;
			}
					
		
			
			function setTaskType(){
				/* var action = $("#action").val();

				if(action!=""&&action!=null){
					var taskValue=action.split("_")[1];
					$("input[name=taskType][value="+taskValue+"]").attr("checked",true);
				
				} */
				var taskType=$(":radio[name='taskType']:checked").val();
				if(taskType=="2"){//如果是自处理任务
				    $('#rolespan').hide();
					$("input[name='type']").attr("disabled","disabled"); 
					$("input[name='taskExeType']").attr("disabled","disabled"); 
					$("#keepDays").attr("disabled","disabled"); 
					$("input[name='crossDept']").attr("disabled",true);
					$("#role").combogrid('clear');
					roleDataFlag = false;
					$("#actionMsgSpan").hide();
				 	$("#actionSpan").show();
					getActions(null,null);
				}else{
					$("input[name='type']").attr("disabled",false); 
					$("input[name='taskExeType']").attr("disabled",false); 
					$("#keepDays").attr("disabled",false); 
					
					var type = $(":radio[name='type']:checked").val();
					if(type=="role"){
						$("input[name='crossDept']").attr("disabled",false); 
						queryRole();
					}else{
						$("input[name='crossDept']").attr("disabled",true);
						queryOwner();
					} 
				}
				
				
			
			}
			
			function closeWin(){
				$("#designProcessWin").window("close");
			}
			//选择角色----
			function queryRole(xmlRoles){
				$('#rolespan').show();
				$("input[name=type][value=role]").attr("checked",true);
				if(!roleDataFlag){
				 getRoles();
				}
				roleDataFlag = true;
				$("input[name='crossDept']").attr("disabled",false); 
				$("input[name='taskExeType']").attr("disabled",false); 
			}
			function queryOwner(){
				$("#actionMsgSpan").hide();
				$("#actionSpan").show();
				$('#rolespan').hide();
				$("input[name=type][value=owner]").attr("checked",true);
				 getActions(null,null);
				$("input[name='crossDept']").attr("disabled",true); 
				$("input[name='taskExeType']").attr("disabled","disabled"); 
			}
			
				$(function(){
				$.extend($.fn.validatebox.defaults.rules, {
					//select即选择框的验证
					selectValid:{
					validator:function(value,param){
						if(value == param[0]){
							return false;
						}else{
							return true ;
						}
					},
					message:'请选择'
				}
			});
		});

			
		</script>
	</head>
	<body class="easyui-layout">
	
	<div region="east" split="true" title="任务节点列表" style="width:230px;padding:10px;">
	<table id="nodeListId"></table>
	<center><input type="button" value="返回" onclick="window.location.href='${ctx}/flow/core/flow-deploy.action?operType=${operType}'"></center>
	</div>
	<div region="center" title="流程图" style="position:relative;">
	<!-- 增图片名称 -->
	<img src="${ctx}/flow/core/flow-deploy!getProcessPic.action?deploymentId=${deploymentId}" style="position:absolute;left:0px;top:0px;">
	<div id="squareId" style="display:none;position:absolute;border:1px solid red;left:${ac.x}px;top:${ac.y}px;width:${ac.width}px;height:${ac.height}px;"></div>
	</div>
	
	<div id="editWin" class="easyui-window" title="发布业务流程资料"
			style="padding: 5px; width:500px; height:240px;"
			iconCls="icon-search" closed="true" maximizable="false"
			minimizable="false" collapsible="false">
			<iframe frameborder="0" id="businessProFrame" height="100%" width="100%"
				scrolling="yes" frameborder="0"></iframe>
		</div>
	
	<div id="designProcessWin" class="easyui-window" title="节点设置"
			style="padding: 5px; width: 650px; height:400px;"
			iconCls="icon-search" closed="true" maximizable="false"
			minimizable="false" collapsible="false" modal="true">
			<div class="easyui-layout" fit="true" border="false">
				<div region="center">
				<form id="setupForm"  name="setupForm" action="" method="POST">
				<input type="hidden" name="sysEname" value="${sysEname}" id="sysEname">
				<table class="datagrid-body">
					<tr>
						<td class="datagrid-header">
							节点ID
						</td>
						<td>
							<input type="text" id="id"  name="id"/>
						</td>
					</tr>
					<tr>
						<td class="datagrid-header">
							节点名称
						</td>
						<td>
							
							<span id="nodeNameId"  style="font-weight: bold;"></span>
						</td>
					</tr>
					<tr>
						<td class="datagrid-header">
							任务类型
						</td>
						<td>
							<input id="checked1" name="taskType" type="radio" value="1" onclick="setTaskType()" />人工处理任务&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input name="taskType" type="radio" value="2"  onclick="setTaskType()" />自处理任务
						</td>
					</tr>
					
					<tr>
						<td class="datagrid-header">
							权限类型
						</td>
						<td>
							<input name="type" type="radio" value="role" onclick="queryRole()"/>角色&nbsp;&nbsp;&nbsp;&nbsp;
							<!-- <input name="type" type="radio" value="user" onclick="queryUser()" checked="checked"/>用户&nbsp;&nbsp;&nbsp;&nbsp; -->
							<input name="type" type="radio" value="owner" onclick="queryOwner()" />经办人
						</td>
					</tr>
					<tr>
						<td class="datagrid-header">
						</td> 
						<td>
							<!-- <span id="userspan">
							 <label>账号：</label> <input type="text" id="hdKeyword" />
							<a class="easyui-linkbutton" iconCls="icon-search"
							onclick="getUsers();">查询</a>&nbsp;<a class="easyui-linkbutton" iconCls="icon-search"
							onclick="clearUSearch();">清空</a>
							<br/> <input id="user" name="user" class="easyui-combogrid"  style="width: 400px;" />
							</span> -->
							<span id="rolespan" style="display:none;">
								<input id="role" name="role" class="easyui-combogrid" required="true" style="width:400px;" />
							</span>
						</td>
					</tr>
					
					<tr>
						<td class="datagrid-header">
							form表单
						</td>
						
						<td>
							  <span id="actionMsgSpan">
						      <font color="#F00" style="font-family:'楷体';" size="4">请先选择角色！</font>
						    </span>
							<span id="actionSpan" style="display:none;">
								<input id="actionAct" name="actionAct" class="easyui-combogrid" required="true" style="width:400px;" />
							</span>
						</td>
					</tr>
					<input type="hidden" id="action" name="action" value=""/>
					
					<tr>
						<td class="datagrid-header">
							任务执行类型
						</td>
						<td>
							<input id="checked2" name="taskExeType" type="radio" value="1" checked="checked" />排它（抢占）&nbsp;&nbsp;
							<input name="taskExeType" type="radio" value="2" />集体（全部同意）&nbsp;&nbsp;
							<input name="taskExeType" type="radio" value="3" />集体（半数同意）&nbsp;&nbsp;
							<input name="taskExeType" type="radio" value="4" />集体（半数同意，全部表决）
						</td>
					</tr>
					<tr>
						<td class="datagrid-header">
							保留天数
						</td>
						<td>
							<input type="text" id="keepDays" name="keepDays">
						</td>
					</tr>
					
					<tr>
						<td class="datagrid-header">
							是否跨部门
						</td>
						<td>
							<input id="checked3" name="crossDept" type="radio" value="1" checked="checked"/>部门内&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input name="crossDept" type="radio" value="2" />跨部门 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input name="crossDept" type="radio" value="3" />机构内&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input name="crossDept" type="radio" value="4" />跨机构&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						</td>
					</tr>
					
					<tr>
						<td colspan="2">
							<a class="easyui-linkbutton" iconCls="icon-add" style=""
								onclick="doSet();">设置</a>
							<a class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeWin();">取消</a>
						</td>
					</tr>
				</table>
				</form>
				</div>
			</div>
		</div>
	</body>
</html>